<template>
    <div>
        <el-form :inline="true" :model="searchInfo" ref="searchInfo">
            <el-form-item label="命令规则">
                <el-input v-model="searchInfo.command" placeholder="请输入命令" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item>
                <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            </el-form-item>
        </el-form>
        <el-form>
            <el-form-item>
                <el-button class="filter-item" type="primary" icon="el-icon-plus" @click="createJumpServerByFilter()">添加</el-button>
            </el-form-item>
        </el-form>
        <el-table
                :data="tableData"
                ref="multipleTable"
                row-key="ID"
                border
                tooltip-effect="dark"
                style="width: 100%">
            <el-table-column label="ID" width="50" align="center" prop="ID">
                <template slot-scope="scope">{{scope.row.ID}}</template>
            </el-table-column>
            <el-table-column prop="command" label="命令"></el-table-column>
            <el-table-column prop="msg" label="告警信息"></el-table-column>
            <el-table-column prop="enable" label="状态">
                <template slot-scope="scope">{{scope.row.enable | basecode(scope.row.enable,'sys_common_status')}}</template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
                <template slot-scope="scope">
                    <el-button @click="updateJumpServerByFilter(scope.row)" icon="el-icon-edit" size="small" type="primary">编辑</el-button>
                    <el-button @click="deleteJumpServerByFilter(scope.row)" type="danger" icon="el-icon-delete" style="margin-left: 6px;" size="mini" >删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                :current-page="page"
                :page-size="pageSize"
                :page-sizes="[10, 30, 50, 100]"
                :style="{float:'right',padding:'20px'}"
                :total="total"
                @current-change="handleCurrentChange"
                @size-change="handleSizeChange"
                layout="total, sizes, prev, pager, next, jumper"
        ></el-pagination>
        <el-dialog :title="dialogTitle"  :close-on-click-modal="false" :visible.sync="dialogFormVisible" width="600px"  @close="closeDialog">
            <el-form ref="appsForm" :model="formData" :rules="rules" size="medium" label-width="110px">
                <el-row :gutter="10">
                    <el-col :span="20" :offset="2">
                        <el-col :span="24">
                            <el-form-item label="命令规则" prop="command">
                                <el-input v-model="formData.command" placeholder="请输入拦截命令" clearable type="textarea"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="告警信息" prop="msg">
                                <el-input v-model="formData.msg" placeholder="请输入告警信息" clearable type="textarea"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="状态" prop="enable">
                                <el-radio-group v-model="formData.enable">
                                    <el-radio
                                            v-for="dict in enableOptions"
                                            :key="dict.item_value"
                                            :label="dict.item_value |formatBoolean"
                                    >{{ dict.item_name }}</el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                    </el-col>
                </el-row>
            </el-form>
            <div class="dialog-footer" slot="footer">
                <el-button @click="closeDialog">取 消</el-button>
                <el-button @click="enterDialog" type="primary">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {
        createJumpServerCmdFilter,
        deleteJumpServerCmdFilter,
        getJumpServerCmdFilterList,
        updateJumpServerCmdFilter,
    }from "@/api/jump_server_filter";
    import infoList from '@/mixins/infoList'
    export default {
        name: "jumpServerFilter",
        mixins: [infoList],
        data() {
            return {
                dialogFormVisible: false,
                dialogTitle: "添加规则",
                enableOptions:[],
                listApi: getJumpServerCmdFilterList,
                searchInfo: {
                    command: null,
                },
                formData: {
                    command: null,
                    msg: null,
                    enable: true
                },
                rules: {
                    command: [{ required: true,  message: '请输入命令', trigger: 'blur' }],
                    msg: [{ required: true,  message: '请输入命令', trigger: 'blur' }],
                    enable: [{ required: true,  message: '请输入命令', trigger: 'blur' }],
                }
            }
        },
        filters: {
            formatBoolean: function(s) {
                if (s == "1" ) {
                    return true
                } else {
                    return false;
                }
            }
        },
        async created() {
            await this.getTableData();
        },
        methods: {
            //搜索
            handleQuery() {
                this.getTableData()
            },
            closeDialog(bool) {
                bool && this.getTableData()
                this.$refs['appsForm'].resetFields()
                this.dialogFormVisible = false
            },
            deleteJumpServerByFilter(row) {
                this.$confirm("此操作将永久删除该应用, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(async () => {
                        this.visible = false;
                        const res = await deleteJumpServerCmdFilter({ ID: row.ID });
                        if (res.code == 0) {
                            this.$message({
                                type: "success",
                                message: "删除成功!"
                            });
                            await this.getTableData();
                        }
                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "已取消删除"
                        });
                    });
            },
            async enterDialog() {
                this.$refs['appsForm'].validate(async valid => {
                    if (!valid) return
                    let res;
                    switch (this.type) {
                        case "create":
                            res = await createJumpServerCmdFilter(this.formData);
                            this.closeDialog(true)
                            break;
                        case "update":
                            res = await updateJumpServerCmdFilter(this.formData);
                            this.closeDialog(true)
                            break;
                        default:
                            res = await createJumpServerCmdFilter(this.formData);
                            this.closeDialog(true)
                            break;
                    }
                    if (res.code == 0) {
                        this.$message({
                            type:"success",
                            message:"创建/更改成功"
                        })
                        this.closeDialog(true);
                        await this.getTableData();
                    }
                })

            },
            async updateJumpServerByFilter(row) {
                this.dialogTitle = '编辑规则';
                this.type = "update";
                this.formData = row;
                this.dialogFormVisible = true;
                this.enableOptions = this.getDicItemList('sys_common_status');
            },
            async createJumpServerByFilter(){
                this.dialogTitle = '新增规则';
                this.type = 'create';
                this.dialogFormVisible = true;
                this.enableOptions = this.getDicItemList('sys_common_status');
            }
        }
    }
</script>

<style scoped>

</style>
